﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;
using Think.Lenovo.Com.Models;
namespace Think.Lenovo.Com.DataAccess
{
    public class MessageBoard
    {
        public int AddMessage(MessageBoardModel model)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            //sql语句
            StringBuilder sql = new StringBuilder(128);
            sql.Append("insert into ")
                .Append(" tbl_messageboard ( Title, Message, AddUserId, AddDate)")
                .Append("values( @Title,@Message, @AddUserId, @AddDate)");

            MySqlParameter[] ps = new MySqlParameter[] {
                    new MySqlParameter("@Title",MySqlDbType.VarChar,100),
                    new MySqlParameter("@Message",MySqlDbType.Text),
                    new MySqlParameter("@AddUserId",MySqlDbType.Int32),
                    new MySqlParameter("@AddDate",MySqlDbType.DateTime)
                };

            ps[0].Value = model.Title;
            ps[1].Value = model.Message;
            ps[2].Value = model.AddUserId;
            ps[3].Value = model.AddDate;
            long id = MysqlHelper.Insert(conn, sql.ToString(), ps);
            return (int)id;
        }
        public List<MessageBoardModel> GetMessages(int pageIndex,int pageSize,String conditions,String orderBy)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder sql = new StringBuilder(512);
            if (pageIndex < 1)
            {
                pageIndex = 1;
            }
            if (pageSize < 1)
            {
                pageSize = 10;
            }
            sql.Append("Select * from tbl_messageboard ");
            if (!String.IsNullOrEmpty(conditions))
            {
                sql.Append(" Where " + conditions);
            }
            sql.Append(" order by Id desc ");
            sql.Append(" limit ")
            .Append((pageIndex - 1) * pageSize)
            .Append(",")
            .Append(pageSize);
            DataSet ds = MySqlHelper.ExecuteDataset(conn, sql.ToString());
            List<MessageBoardModel> list = new List<MessageBoardModel>();
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        DateTime dt;
                        Int32 i;
                        MessageBoardModel model = new MessageBoardModel();
                        model.Id = Convert.ToInt32(row["Id"]);
                        DateTime.TryParse(row["AddDate"].ToString(), out dt);
                        model.AddDate = dt;
                        Int32.TryParse(row["AddUserId"].ToString(), out i);
                        model.AddUserId = i;

                        model.Message = Convert.ToString(row["Message"]);
                        model.Title = Convert.ToString(row["Title"]);
                        list.Add(model);
                    }
                }
            }
            return list;
        }
    }
}
